<template>
  <div class="areaContainer">
    <div class="search">
      <el-input v-model.trim="searchValue" placeholder="请输入反馈内容或状态关键字进行查询" clearable :prefix-icon="Search" @keyup="getAreaList(searchValue)">
        <template #append>
          <el-button :icon="Search"  @click="getAreaList(searchValue)"/>
        </template>
      </el-input>
    </div>
    <el-table :data="recordData" stripe style="width: 100%;margin-top: 10px;" height="50%" :default-sort="{ prop: 'createTime', order: 'descending' }">
    <el-table-column prop="feedbackId" label="反馈ID"/>
    <el-table-column prop="userId" label="反馈人ID"/>
    <el-table-column prop="nickName" label="反馈人昵称" />
    <el-table-column label="反馈人头像">
      <template #default="{row}">
        <img :src="row.avatarUrl" alt="" style="width: 50px;height: 50px" />
      </template>
    </el-table-column>
    <el-table-column prop="statusName" label="状态" :style="setStatusColor" />
    <el-table-column prop="feedbackContent" label="反馈内容" />
    <el-table-column prop="dealMessage" label="处理信息" />
    <el-table-column prop="createTime" label="反馈时间" width="200" sortable>
        <template #default="row">
        <div>{{timeTuTIL.myTimeFormat(row.row.createTime)}}</div>
      </template>
    </el-table-column>
    <el-table-column fixed="right" label="操作" width="130">
      <template #default="row">
          <el-tooltip content="删除" placement="bottom">
            <el-button link type="danger"  class="myLink" :icon="Delete" @click="collegeDelete(row)"></el-button>
          </el-tooltip>
          <el-tooltip content="处理" placement="bottom">
            <el-button link type="primary"  class="myLink" :icon="Edit" @click="deal(row)"></el-button>
          </el-tooltip>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog v-model="editCollegeVisible" title="处理" draggable>
    <el-form :model="editCollegeForm" label-width="80px">
      <el-form-item label="反馈ID" prop="feedbackId">
        <el-input v-model="editCollegeForm.feedbackId" autocomplete="off" disabled/>
      </el-form-item>
      <el-form-item label="处理内容" prop="dealMessage">
        <el-input v-model="editCollegeForm.dealMessage" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="editCollegeVisible = false">取消</el-button>
        <el-button type="primary" @click="collegeEdit()">提交处理</el-button>
      </span>
    </template>
  </el-dialog>
   <div class="pagination">
    <el-pagination v-model:current-page="page" v-model:page-size="size" :page-sizes="[5,10,20,30]" :small="small" :disabled="disabled"
      :background="background" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
  </div>
  </div>
</template>

<script setup>
import { reactive , inject , onMounted , ref } from "vue"
import axios from '../../axios/axiosInstance'
import timeTuTIL from '../../utils/timeUtilT'
import { ElLoading,ElMessage ,ElMessageBox} from 'element-plus'
import { Delete ,Edit,Search} from '@element-plus/icons-vue'

//变量定义
let recordData = reactive([])
let searchValue = ref()

const total = ref(0)
let page = 1
let size = 5
const small = ref(false)
const background = ref(false)
const disabled = ref(false)
const editCollegeVisible = ref(false)
let editCollegeForm = reactive({
  feedbackId: '',
  userId: '',
  feedbackContent: '',
  feedbackDate: "",
  statusCode: "",
  statusName: "",
  dealMessage: "",
  createTime: "",
})
//方法 

const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
  size = val
  init()
}

const setStatusColor = ({ row, column, rowIndex, columnIndex }) => {
       if (row.statusName === '待处理') {
         return 'status-pending'; // 红色样式
       } else if (row.statusName === '已处理') {
         return 'status-completed'; // 绿色样式
       }
       return '';
     }
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  page = val
  init()
}

 const clear = (value) => {
    Object.keys(value).map(key => {
      delete value[key]
    })
  }
  
  const deal = (row) => {
  editCollegeVisible.value = true;
  console.log("row为："+row);
  editCollegeForm.feedbackId = row.row.feedbackId;
  editCollegeForm.userId = row.row.userId;
  editCollegeForm.feedbackContent = row.row.feedbackContent;
  editCollegeForm.feedbackDate = row.row.feedbackDate;
  editCollegeForm.statusCode = row.row.statusCode;
  editCollegeForm.statusName = row.row.statusName;
  editCollegeForm.dealMessage = row.row.dealMessage;
  editCollegeForm.createTime = row.row.createTime;
  console.log(editCollegeForm)
}

  const collegeDelete = (row) => {
  console.log("删除的row为：")
  console.log(row)
  let nickName = row.row.nickName
  let feedbackId = row.row.feedbackId
  ElMessageBox.confirm(
    '确定要删除用户【'+nickName+'】提出的反馈内容吗？',
    'Warning',
    {
      confirmButtonText: 'OK',
      cancelButtonText: 'Cancel',
      type: 'warning',
    }
  )
    .then(() => {
      axios.delete('/feedback/'+feedbackId).then((res) => {
        console.log(res)
        if(res.code == 200){
          ElMessage({type: "success",message: "删除成功"});
          recordData.pop(row.$index)
          init()
        }else{
          ElMessage({type: "error",message: "删除失败"});
       }
      }).catch((err) => {
        console.log(err)
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '删除取消',
      })
    }) 
}

const collegeEdit = () => {
   axios.post('/feedback/deal',editCollegeForm).then((res) => {
    console.log(res)
    if(res.code == 200){
      ElMessage({type: "success",message: "处理成功"});
      editCollegeVisible.value = false;
      init();
    }else{
      ElMessage({type: "error",message: "处理失败"});
    }
  }).catch((err) => {
    console.log(err)
  })
}

const getAreaList = (searchValue) => {
  if(searchValue != ''){
    axios.post(`/feedback/listByParam`,{
      page: page,
      size: size,
      search: searchValue,
    }).then((res) => {
        console.log(res)    
        if(res.code == 200){
          clear(recordData)
          total.value = res.data.total;
          for(let i in res.data.records){
            console.log(i)
            recordData.push(res.data.records[i])
          }
          ElMessage({type: "success",message: "查询成功"});
        }else{
          ElMessage({type: "error",message: "查询失败"});
       }
      }).catch((err) => {
        console.log(err)
      })
  }else{
    init()
  }
}

const init = () => {
  const loading = ElLoading.service({
        lock: true,
        text: 'Loading',
          background: 'rgba(0, 0, 0, 0.7)',
      })
      setTimeout(() => {
        loading.close()
      }, 2000)
      axios.post(`/feedback/listByParam`,{
      page: page,
      size: size,
    }).then((res) => {
        console.log(res)    
        if(res.code == 200){
          clear(recordData)
          total.value = res.data.total;
          for(let i in res.data.records){
            console.log(i)
            recordData.push(res.data.records[i])
          }
          ElMessage({type: "success",message: "查询成功"});
        }else{
          ElMessage({type: "error",message: "查询失败"});
       }
      }).catch((err) => {
        console.log(err)
      })
}

//页面初始化方法
onMounted(()=>{ 
  init()
})
</script>

<style scoped>
.areaContainer .search{
  display: flex;
  width: 600px;
}
.areaContainer .pagination{
 justify-content: center;
 margin-top: 15px;
}
.status-pending {
     color: red;
}
.status-completed {
     color: green;
}

</style>
